import axios from "axios";
import {ElMessage} from "element-plus";
import useUserStore from '@/store/mod/user.ts'
//在此阶段声明变量接收store，会报错，因为pinia在此还未加载完成。
// const $userStore = useUserStore()

const request = axios.create({
    baseURL:'',
    timeout:5000
})
request.interceptors.request.use((config)=>{
    const $userStore = useUserStore()
    if($userStore.userInfo.token){
        config.headers.token= $userStore.userInfo.token
    }
    return config
},(err)=>{
    return Promise.reject(new Error(err.message))
})
request.interceptors.response.use((res)=>{
    return res.data
},(err)=>{
    let status = err.response.status
    switch (status){
        case 404:
            ElMessage({
                type:'error',
                message:err.message
            })
            break;
        case 500|501|502|503|504|505:
            ElMessage({
                type:'error',
                message:'服务器挂了'
            })
            break;
        case 401:
            ElMessage({
                type:'error',
                message:'参数有误'
            })
            break;
    }
    return Promise.reject(new Error(err.message))
})
export  default  request